﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using GameClient;
namespace Submarines
{
    class Computer : Board
    {
        StepResult _Step;

        void DisableControls()
        {
            frmGame form = this.Parent as frmGame;

            form.Controls["tblOpponent"].Enabled = false;
            form.Controls["tblPlayer"].Enabled = false;
            form.Controls["btnResign"].Enabled = false;
        }

        void EnableControls()
        {
            frmGame form = this.Parent as frmGame;

            form.Controls["tblOpponent"].Enabled = true;
            form.Controls["tblPlayer"].Enabled = true;
            form.Controls["btnResign"].Enabled = true;
        }
        
        protected override void OnMouseClick(object sender, MouseEventArgs e)
        {
            frmGame form = this.Parent as frmGame;
            
            if ((e.Button & MouseButtons.Left) != MouseButtons.Left) return;
            Button button = ((Button)sender);
            if (Global._Local)
            {
                if ((CellStatus)button.Tag == CellStatus.PartOfSubmarine)
                {
                    int x = GetX(button.Name);
                    int y = GetY(button.Name);
                    _Step = TryToHit(x, y);
                }
                else
                {
                    if ((CellStatus)button.Tag != CellStatus.Hit) button.Visible = false;
                    _Step = StepResult.Empty;
                }

                if (_Step != StepResult.Hit && _Step != StepResult.Killed)
                    form.AI_Hit();

                if (IsGameOver())
                {
                    Global.stop_game = true;
                }
            }
            else
            {
                int x = GetX(button.Name);
                int y = GetY(button.Name);
                Diag.AddLog(this.Name, "Client.GetHit " + x + "," + y + " sent");
                GameClient.Client.GetHit(x, y);
            }
            Global.ClearTimer();
        }

        void MarkCell(int x, int y, string status)
        {
            Button btn = (Button)tblPanel.GetControlFromPosition(x, y);
            switch (status)
	        {
                case "Empty": 
                    btn.Visible = false;
                    btn.Tag = CellStatus.Empty;
                    break;
                case "Hit" :
                    btn.BackColor = _HitColor;
                    btn.Tag = CellStatus.Hit;
                    break;
                case "Killed" :
                    btn.BackColor = _HitColor;
                    btn.Tag = CellStatus.Hit;
                    CheckIfKilled(x, y);
                    break;
                case "GameOver":
                    MessageBox.Show("You win");
                    break;
	        }
        }
 
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Computer));
            this.SuspendLayout();
            // 
            // Computer
            // 
            resources.ApplyResources(this, "$this");
            this.DoubleBuffered = true;
            this.Name = "Computer";
            this.Load += new System.EventHandler(this.Computer_Load);
            this.ResumeLayout(false);

        }

        private void Computer_Load(object sender, EventArgs e)
        {
            if (!Global._Local)
            {
                GameClient.Client.HitAnswer += new Client.GameReceiveHandler(Client_HitAnswer);
                //GameClient.Client.MoveReceived += new Client.GameReceiveHandler(Client_MoveReceived);  ?????????????????
            }
        }

        void ShowMessage(string Message)
        {
            frmGame form = this.Parent as frmGame;
            ListBox list = form.Controls["MessageList"] as ListBox;

            list.Items.Add(Message);
        }
        
        void Client_HitAnswer(object sender, ClientEventArgs e)
        {
            Diag.AddLog(this.Name, "Client_HitAnswer " + e.ColValue + "," + e.RowValue + "=" + e.StringValue + " recieved");
            MarkCell(e.ColValue, e.RowValue, e.StringValue);

            ShowMessage("Hit answered: " + e.StringValue);
        }

        void Client_MoveReceived(object sender, ClientEventArgs e)
        {
            StepResult result = TryToHit(e.ColValue, e.RowValue);
            GameClient.Client.HitResult(e.ColValue,e.RowValue,result.ToString());

            ShowMessage("Move recieved: " + e.StringValue);
        }

        

    }
}



